<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="景点名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入景点名称"
          clearable
          @keyup.enter.native="handleQuery"
          :trigger-on-focus="false"
        />
      </el-form-item>
      <el-form-item label="景点类型" prop="sceneryType">
        <el-select v-model="queryParams.sceneryType" placeholder="请选择景点类型" clearable>
          <el-option
            v-for="dict in dict.type.travel_scenery_tab"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="星级" prop="star">
        <el-input
          v-model="queryParams.star"
          placeholder="请输入星级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推荐指数" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入推荐指数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="票价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入票价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['travel:scenery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['travel:scenery:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['travel:scenery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['travel:scenery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sceneryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="景点名称" align="center" prop="name" />
      <el-table-column label="景点类型" align="center" prop="sceneryType">
        <template slot-scope="scope">
<!--          <dict-tag :options="dict.type.travel_scenery_tab" :value="scope.row.sceneryType ? scope.row.sceneryType.split(',') : []"/>-->
          {{ selectDictLabels(dict.type.travel_scenery_tab, scope.row.sceneryType, ",") }}
        </template>
      </el-table-column>
      <el-table-column label="星级" align="center" prop="star" />
      <el-table-column label="推荐指数" align="center" prop="score" />
      <el-table-column label="票价" align="center" prop="price" />
      <el-table-column label="更新人" align="center" prop="updateNo" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['travel:scenery:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['travel:scenery:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改景点信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="24">
            <el-form-item label="景点类型" prop="sceneryType">
              <el-select v-model="form.sceneryType" placeholder="请选择景点类型" multiple clearable style="width:100%;">
                <el-option
                  v-for="dict in dict.type.travel_scenery_tab"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="行政区" prop="district">
              <el-select v-model="form.district" value-key="id" placeholder="请选择行政区" clearable filterable style="width:100%;">
                <el-option
                  v-for="dict in districtArr"
                  :key="dict.name"
                  :label="dict.name"
                  :value="dict.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="17">
            <el-form-item label="景点名称" prop="name">
              <!--
                   debounce	获取输入建议的去抖延时
                   trigger-on-focus 是否在输入框 focus 时显示建议列表，false时聚焦不触发
                   select-when-unmatched 在输入没有任何匹配建议的情况下，按下回车是否触发 select 事件
                   highlight-first-item 是否默认突出显示远程搜索建议中的第一项
                   prefix-icon	输入框头部图标
                   prefix-icon	输入框头部图标
                   hide-loading	是否隐藏远程加载时的加载图标
                   highlight-first-item	是否默认突出显示远程搜索建议中的第一项
              -->
              <el-autocomplete
                style="width:100%;"
                class="inline-input"
                v-model="form.name"
                :fetch-suggestions="querySuggestion"
                placeholder="请输入景点名称"
                @select="handleSuggestion"
                :trigger-on-focus="true"
                :select-when-unmatched="true"
                :highlight-first-item="true"
                :debounce="100"
                clearable
                @clear="clearSuggestion"

              ></el-autocomplete>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="位置描述" prop="addressDesc">
              <el-input v-model="form.addressDesc"  placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入维度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="星级" prop="star">
              <el-input v-model="form.star" placeholder="请输入星级" value="5"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推荐指数" prop="score">
              <el-input v-model="form.score" placeholder="请输入推荐指数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票价" prop="price">
              <el-input v-model="form.price" placeholder="请输入票价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="form.status" placeholder="请选择状态(00有效，01无效)" clearable style="width:100%;">
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="景点地图">
              <image-upload v-model="form.map"/>
            </el-form-item>
          </el-col>
<!--          <el-col :span="24">-->
<!--            <el-form-item label="景点地图" prop="map">-->
<!--              <el-input v-model="form.map" placeholder="请输入景点地图" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="推荐原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="desc">
              <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="注意事项" prop="note">
              <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScenery, getScenery, delScenery, addScenery, updateScenery, suggestion, getchildren} from "@/api/travel/scenery";

export default {
  name: "Scenery",
  dicts: ['travel_scenery_tab', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 景点信息表格数据
      sceneryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      districtArr:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sceneryType: null,
        star: null,
        score: null,
        price: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "维度不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "行政区不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "景点名称不能为空", trigger: "blur" }
        ],
      },
      mapAddress : null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询景点信息列表 */
    getList() {
      this.loading = true;
      listScenery(this.queryParams).then(response => {
        this.sceneryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sceneryType: [],
        district: null,
        address: null,
        addressDesc: null,
        longitude: null,
        latitude: null,
        star: null,
        score: null,
        reason: null,
        desc: null,
        note: null,
        map: null,
        price: null,
        remark: null,
        status: "0",
        flag: null,
        createNo: null,
        createTime: null,
        updateNo: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getchildren().then(
        response => {
          this.districtArr = response.result[0];
          console.log(response)
        }
      )
      this.reset();
      this.open = true;
      this.title = "添加景点信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getchildren().then(
        response => {
          this.districtArr = response.result[0];
        }
      )
      getScenery(id).then(response => {
        this.form = response.data;
        this.form.sceneryType = this.form.sceneryType.split(",");
        this.open = true;
        this.title = "修改景点信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sceneryType = this.form.sceneryType.join(",");
          if (this.form.id != null) {
            updateScenery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScenery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除景点信息编号为"' + ids + '"的数据项？').then(function() {
        return delScenery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('travel/scenery/export', {
        ...this.queryParams
      }, `scenery_${new Date().getTime()}.xlsx`)
    },

    // 返回的搜索内容事件处理
    querySuggestion(queryString, cb) {
      // let dataList = [ // 也可以在后端接口动态获取
      //   {id: 1, value: "111"},
      //   {id: 2, value: "222"},
      //   {id: 3, value: "333"},
      //   {id: 4, value: "444"}
      // ]
      let dataList = []
      if (queryString === '') {
        cb(dataList)
      } else {
        suggestion({keyword:queryString,region:this.form.district}).then(response => {
          dataList = response.data
          for(let idx in dataList) {
            dataList[idx]['value'] = dataList[idx]['title'] + '(' +dataList[idx]['address'] + ')'
          }
          // 调用 callback 返回建议列表的数据
          cb(dataList);
        });
      }
    },
    // 选中输入框推荐的值的时候触发
    handleSuggestion(item) {
      console.log(item);
      this.form.name = item.title
      this.form.address = item.address
      this.form.longitude = item.location.lng
      this.form.latitude = item.location.lat
    },
    // 点击clearable清空小图标按钮以后，继续重新在输入框中输入数据，querySearch会触发，但是cb函数不会触发
    // 这样的话就会出现发请求了，也获取数据了，但是input框的输入建议下拉框不呈现在页面上的问题，所以解决方法就是
    // 只要用户点击了
    clearSuggestion(){
      document.activeElement.blur()
    },

  }
};
</script>
